//
//  YKBasicInfoViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/9.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKBasicInfoViewController.h"
#import "YKInputView.h"
#import "YKTabBarViewController.h"
@interface YKBasicInfoViewController ()

@end

@implementation YKBasicInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"基本资料";
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self updateViews];
}

- (void)updateViews {
    [self.view tf_removeAllSubViews];
    YKInputView *userAccountView = [[YKInputView alloc]initWithTitle:@"用户账号" input:[YKTabBarViewController sharedInstance].userModel.ID];
    userAccountView.editable = NO;
    [self.view addSubview:userAccountView];
    
    YKInputView *userNameView = [[YKInputView alloc]initWithTitle:@"用户姓名" input:[YKTabBarViewController sharedInstance].userModel.USER_NAME];
    userNameView.editable = NO;
    [self.view addSubview:userNameView];
    
    YKInputView *belongGroupView = [[YKInputView alloc]initWithTitle:@"所属组织" input:[YKTabBarViewController sharedInstance].userModel.ORG_NAME];
    belongGroupView.editable = NO;
    [self.view addSubview:belongGroupView];
    
    YKInputView *belongRoleView = [[YKInputView alloc]initWithTitle:@"所属角色" input:@""];
    belongRoleView.editable = NO;
    [self.view addSubview:belongRoleView];
    
    [self.view.subviews mas_alignAlongAxis:MASAxisTypeVertical fixedSpace:@13 leadSpace:10];
    [self.view.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(15);
        make.trailing.equalTo(self.view.mas_trailing).offset(-15);
    }];
    
    
    UIButton* confirmButton = [[UIButton alloc]init];
    [confirmButton setTitle:@"注销" forState:UIControlStateNormal];
    [confirmButton setBackgroundImage:TFSTYLE(navigationBarBackgroudImage) forState:UIControlStateNormal];
    
    [confirmButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor tf_colorWithHexString:@"#999999"]] forState:UIControlStateDisabled];
    
    [confirmButton setContentEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    confirmButton.tf_cornerRadius = 4.0f;
    
    [self.view addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view.mas_leading).offset(15);
        make.trailing.equalTo(self.view.mas_trailing).offset(-15);
        make.top.equalTo(belongRoleView.mas_bottom).offset(20);
    }];
    
    confirmButton.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        [TFHTTPMANAER POST:@"login/exit.htm" parameters:nil HUD:nil success:^(id responseObject) {
            [YKTabBarViewController sharedInstance].userModel = nil;
            [[YKTabBarViewController sharedInstance]viewDidAppear:NO];
        } failure:^(id responseObject, NSError *error) {
            [YKTabBarViewController sharedInstance].userModel = nil;
            [[YKTabBarViewController sharedInstance]viewDidAppear:NO];
        }];
        return [RACSignal empty];
    }];
    
    UILabel *versionLabel = [[UILabel alloc]init];
    [self.view addSubview:versionLabel];
    [versionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-30);
    }];
    versionLabel.text = [NSString stringWithFormat:@"系统版本V%@",[[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
    
    
    [self tf_showStateView:kTFViewStateLoading];
    YKTabBarViewController *tab = (YKTabBarViewController *)self.tabBarController;
    [TFHTTPMANAER POST:@"personalCentr/searchBasicDataByUserId.htm" parameters:@{@"id": tab.userModel.ID} success:^(id responseObject) {
        NSString * USER_ID = [responseObject valueForKeyPath:@"userInfo.USER_ID"];
        NSString * USER_NAME = [responseObject valueForKeyPath:@"userInfo.USER_NAME"];
        NSString * PART_NAME = [responseObject valueForKeyPath:@"userInfo.PART_NAME"];
        NSString * ROLE_NAME = [responseObject valueForKeyPath:@"userInfo.ROLE_NAME"];
        
        userAccountView.input = USER_ID;
        userNameView.input = USER_NAME;
        belongGroupView.input = PART_NAME;
        belongRoleView.input = ROLE_NAME;
        
        [self tf_showStateView:kTFViewStateNone];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
}

@end
